package com.xjtu.project.service.impl;

import com.xjtu.project.mapper.ReportMapper;
import com.xjtu.project.model.vo.SalesTop10ReportVO;
import com.xjtu.project.model.vo.TurnoverReportVO;
import com.xjtu.project.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        LocalDateTime b = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime e = LocalDateTime.of(end, LocalTime.MAX);
        List<Map<String, Object>> map = reportMapper.getSalesTop10(b, e);
        //这里也可以不用map 用GoodSalesDTO来封装对应的字段name 和 number然后返回的就是他的一个list
        //map里面封装的有两个 一个是name字段对应名字 一个是number字段对应数量
        List<Object> list1 = map.stream().map(m -> m.get("name")).collect(Collectors.toList());
        List<Object> list2 = map.stream().map(m -> m.get("number")).collect(Collectors.toList());
        return new SalesTop10ReportVO(list1, list2);
    }

    @Override
    public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.isEqual(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //接下来要根据dateList内封装的日期去找
        List<BigDecimal> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            BigDecimal turnover = reportMapper.getTurnover(beginTime, endTime);
            System.out.println(turnover == null ?null:turnover.doubleValue());
            turnoverList.add(turnover);
        }
        return new TurnoverReportVO(dateList, turnoverList);
    }
}
